package com.etc.service.impl;

import com.etc.mapper.IFriendMapper;
import com.etc.pojo.Friend;
import com.etc.pojo.User;
import com.etc.service.IUserService;
import com.etc.service.IfriendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: RookieSocialNetworking
 * @description: 好友的实现类
 * @author: Mr.单
 * @create: 2020-08-18 10:58
 **/
@Service
public class IfriendServiceImpl implements IfriendService {
    @Autowired
    IFriendMapper friendMapper;
    @Autowired
    IUserService userService;

    @Override
    public PageInfo<Friend> getFriendByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Friend> list = friendMapper.queryFriendByUserId(userId);
        PageInfo<Friend> friendPageInfo = new PageInfo<Friend>(list);
        return friendPageInfo;
    }

    @Override
    public PageInfo<Friend> getFriendApply(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Friend> list = friendMapper.queryFriendApply(userId);
        PageInfo<Friend> pageInfo = new PageInfo<Friend>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Friend> getByNameandTel(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Friend> list = friendMapper.queryByNameandTel(user);
        for (Friend friend : list) {
            if (friend.getFriend().getUserId() == user.getUserId()) {
                list.remove(friend);
            }
        }
        PageInfo<Friend> pageInfo = new PageInfo<Friend>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Friend> queryMyFriendByNameandTel(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Friend> list = friendMapper.queryMyFriendByNameandTel(user);
        PageInfo<Friend> pageInfo = new PageInfo<Friend>(list);
        return pageInfo;
    }

    @Override
    public boolean addFriendok(Integer friendId, Integer userId,Integer fs) {
        return friendMapper.addFriend(friendId, userId,fs) == 0 ? false : true;
    }


    @Override
    public Integer getFriendCount(Integer userId) {
        return friendMapper.queryFriendCount(userId);
    }

    @Override
    public Boolean updateFriend(Friend friend) {
        return friendMapper.updateFriend(friend) == 0 ? false : true;
    }
}
